package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper mapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = mapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return mapper.getDishItemBySetmealId(id);
    }



    public PageResult queryPaeg(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = mapper.getPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public SetmealVO getMeal(Long id) {
        //1、根据id查询setmeal基本数据,
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = mapper.getMealById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据dishid查询setmealDish这个集合
        List<SetmealDish> querySetMealDish = mapper.getSetMealWithDishById(setmeal.getId());
        setmealVO.setSetmealDishes(querySetMealDish);

        return setmealVO;
    }
    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //新增一条套餐信息
        //第一步：先添加套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        mapper.saveBaseSetMeal(setmeal);
        //进行数据回显
        Long setMealId = setmeal.getId();

        //第二步：将SetMealDish这个集合从里面拿出来，
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes.size()>0&&setmealDishes!=null){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setMealId);
            }
            //批量插入
            mapper.insertSetMeal(setmealDishes);
        }
    }

    /**
     * 修改起售、停售
     * @param status
     * @param id
     */
    public void updateStatus(Integer status, Integer id) {
        mapper.updateStatusById(status,id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        if(ids!=null&&ids.size()>0){
            mapper.deleteBatch(ids);
        }
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        log.info("业务层：更新菜品套餐及其菜品信息{}",setmealDTO);
        //第一步：先更新基本数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        mapper.updateBaseSetMeal(setmeal);
        //第二步：获取菜品集合，然后删除和套餐包含的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        mapper.deleteSetMealWithDish(setmeal.getId());
        //第三步：新增菜品信息
        if(setmealDishes.size()>0&& setmealDishes!=null){
            //首先新增套餐 id，采用批量插入
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            //调用批量插入方法
            mapper.insertSetMeal(setmealDishes);
        }

    }
}
